#include "Game.h"
#include "Round.h"
#include "Stack.h"
#include "GameScore.h"
#include "UI.h"

void
game_play()
{
	// Create GameScore structure for this game
  // 0 games played, 0 wins, 1000 chips
  GameScore score = { 0, 0, 1000 };

  // Create Stack structure for this game
  Stack stack;
  // Fill the stack up with five ordered decks
  stack_initialize(&stack);
  // Shuffle stack three times
  stack_shuffle(&stack, 3);

  while (1) {
    // Ask the player for his bet
    unsigned long bet = ui_get_bet(score.Chips);

    // Play a round of blackjack
    unsigned char result = round_play(&stack);

    // Adjust score structure according to round result
    score.Rounds++;
    if (result == 2) {
      score.Chips += bet;
      score.Wins++;
    } else if (result == 0) {
      score.Chips -= bet;
    }

    // If the player is out of chips
    if (score.Chips == 0) {
      // .. leave the game
      ui_game_over();
      return;
    }

    result = ui_scores(score);
    if (result == 0)
      return;

    // If there are less then ten cards left on the stack
    if (stack_remaining_cards(&stack) < 10)
      // .. refill stack and shuffle
      stack_shuffle(&stack, 1);
  }
}
